<?php
// src/Pyaf/GymBundle/Controller/AdminCalendarioController.php
namespace Pyaf\GymBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Pyaf\GymBundle\Entity\Calendario;
use Pyaf\GymBundle\Entity\PaqueteRutina;
use Pyaf\GymBundle\Entity\Rutina;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;


/**
 * Description of AdminCalendarioController
 *
 * @author Toshiba P755-S5272
 */
class AdminCalendarioController extends Controller{

    public function listAction(){
        $em = $this->getDoctrine()->getEntityManager();
        $paquetesRutinas = $em->getRepository('PyafGymBundle:PaqueteRutina')->findAllOrderByNombre();
        return $this->render('PyafGymBundle:AdminCalendario:list.html.twig', array('paquetesRutinas' => $paquetesRutinas));
    }
    
    public function showAction($id){
        $paqueteRutina = new PaqueteRutina(); 
        $paqueteRutina = $this->getDoctrine()->getEntityManager()
                         ->getRepository('PyafGymBundle:PaqueteRutina')
                         ->findPaqueteRutina($id);
        $rutinas = $paqueteRutina->getRutinas();
        return $this->render('PyafGymBundle:AdminCalendario:show.html.twig', array('rutinas' => $rutinas));
    }

    public function addEventAction(){
        date_default_timezone_set('America/Argentina/Buenos_Aires');
        $peticion = $this->getRequest();
        $calendario = new Calendario();
        
        $pieces = explode("-", $peticion->request->get('title'));
        
        $calendario->setIdRutina($pieces[0]);
        $calendario->setTitle($pieces[1]);
        $calendario->setStart(date('Y-m-d H:i:s', $peticion->request->get('date_start')));
        $calendario->setEnd(date('Y-m-d H:i:s', $peticion->request->get('date_start')));
        $calendario->setEditable(true);
        $calendario->setAllDay(true);
        
        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($calendario);
        $em->flush();

	$em->clear();
        $calendario2 = $em->getRepository('PyafGymBundle:Calendario')->findJsonDate($calendario->getId());
        
        $jsonData = json_encode(array('success' => true, 'event' => $calendario2));
        $response = new Response($jsonData);
        return $response;
    }
    
    public function deleteEventAction(){
        
        $peticion = $this->getRequest();
        $em = $this->getDoctrine()->getEntityManager();
        $calendario = $em->getRepository('PyafGymBundle:Calendario')->find($peticion->request->get('id'));
        $em->remove($calendario);
        $em->flush();
        
        $jsonData = json_encode(array('success' => true));
        $response = new Response($jsonData);
        return $response;
    }
    
    public function updateEventAction(){
        date_default_timezone_set('America/Argentina/Buenos_Aires');
        $peticion = $this->getRequest();
        $calendario = new Calendario();
        $em = $this->getDoctrine()->getEntityManager();
        $calendario = $em->getRepository('PyafGymBundle:Calendario')->find($peticion->request->get('id'));
                
        $calendario->setTitle($peticion->request->get('title'));
        $calendario->setStart(date('Y-m-d H:i:s', $peticion->request->get('start')));
        $calendario->setEnd(date('Y-m-d H:i:s', $peticion->request->get('start')));
               
        $em->persist($calendario);
        $em->flush();

    }
    
     /**
     * Metodo que lee de la base de datos el calendario y lo transforma en formato
     * Json para luego ser renderizado
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function jsonDataAction(){
        $em = $this->getDoctrine()->getEntityManager();
        $calendario = $em->getRepository('PyafGymBundle:Calendario')->retrieveAllEvents();
        
        $jsonData = json_encode($calendario);
        $response = new Response($jsonData);
        return $response;
        
    }
    
}

?>
